package com.imjohn.controller;

import com.github.pagehelper.PageInfo;
import com.imjohn.entity.Aticle;
import com.imjohn.entity.Teacher;
import com.imjohn.service.PageService;
import com.imjohn.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangwei
 * @date 2021-05-06 下午 3:24
 */
@RestController
@RequestMapping("/api")
public class Test {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private PageService pageService;
    @RequestMapping("/getTeacher")
    @ResponseBody
    public Teacher getTeacher() {
        return teacherService.getTeacherById(1);
    }
    @RequestMapping("/test")
    @ResponseBody
    public String hello() {
        int[] ns = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 };
        for (int i =0; i < ns.length -1; i++) {
            for (int j = 0; j < ns.length - i - 1; j++) {
                if (ns[j] > ns[j+1]) {
                    // 交换ns[j]和ns[j+1]:
                    int tmp = ns[j];
                    ns[j] = ns[j+1];
                    ns[j+1] = tmp;
                }
            }
        }
        return Arrays.toString(ns);
    }
    @RequestMapping("/index")
    @ResponseBody
    public Map index(@RequestBody Map model) {
        return model;
    }
    @RequestMapping("/selectAll")
    @ResponseBody
    public Map selectAll(@RequestBody Map requestParams) {
        int pageNum;
        int pageSize;
        if(requestParams.get("pageNum").toString() != "") {
             pageNum = Integer.parseInt(requestParams.get("pageNum").toString());
        } else {
             pageNum = 1;
        }
        if(requestParams.get("pageSize").toString() != "") {
            pageSize = Integer.parseInt(requestParams.get("pageNum").toString());
        } else {
            pageSize = 10;
        }
        PageInfo<Aticle> pageInfo = pageService.GetAllAticle(pageNum, pageSize);
        System.out.println("当前页：" + pageInfo.getPageNum());
        System.out.println("每页显示的数量：" + pageInfo.getPageSize());
        System.out.println("当前页的数量：" + pageInfo.getSize());
        System.out.println("总记录数：" + pageInfo.getTotal());
        System.out.println("总页数：" + pageInfo.getPages());
        System.out.println("结果集:" + pageInfo.getList());
        System.out.println("第一页:" + pageInfo.getNavigateFirstPage());
        System.out.println("前一页:" + pageInfo.getPrePage());
        System.out.println("下一页:" + pageInfo.getNextPage());
        System.out.println("最后一页:" + pageInfo.getNavigateLastPage());
        System.out.println("是否为第一页：" + pageInfo.isIsFirstPage());
        System.out.println("是否为最后一页：" + pageInfo.isIsLastPage());
        System.out.println("是否有前一页：" + pageInfo.isHasPreviousPage());
        System.out.println("是否有下一页：" + pageInfo.isHasNextPage());
        System.out.println("导航页码数" + pageInfo.getNavigatePages());
        System.out.println("所有导航页号" + Arrays.toString(pageInfo.getNavigatepageNums()));
        Map res = new HashMap();
        List<Aticle> iMap =  pageInfo.getList();
        if (iMap.size() > 0) {
            res.put("list" ,iMap);
            res.put("total" ,pageInfo.getTotal());
            res.put("respCode" ,"00000000");
        } else {
            res.put("respCode", "12345678");
            res.put("respMsg", "失败");
        }
        return res;
    }
}
